(function ($) {

    var kfid = '';

    var link = '&nbsp;&nbsp;&nbsp;<a id="agreelink" href = "https://cybozukf5.oss-cn-shanghai.aliyuncs.com/files/kntone%E5%BC%80%E5%8F%91%E8%AE%B8%E5%8F%AF%E8%AF%81%E5%8D%8F%E8%AE%AE_20190805.pdf" target="_blank">许可协议</a>';
    window.addEventListener('message', function (event) {
        kfid = event.data;
    })

    fb.events.form.submit = [function (state) {
        state.record.kfid.value = kfid;
        var domain = createDomain();
        state.record.domain.value = domain;
        return state;
    }];

    fb.events.form.mounted = [function (state) {
        //cn环境无法使用getElementByCode?
        //var agreeElement = fb.getElementByCode("agree");
        var $agreeElement = $(".el-checkbox__label");
        if ($("#agreelink").length > 0) {
            $("#agreelink").remove();
        }
        $agreeElement.append(link);
        return state;
    }];

    fb.events.form.created.push(function (state) {
        state.fields[1].validations.push({
            params: [],
            rule: 'email'
        });
        state.fields[3].validations.push({
            params: [],
            rule: 'mobile'
        });
    });

    fb.addValidators = function (state) {
        return {
            email: {
                getMessage: function (fieldCode, params) {
                    return '邮件格式不正确';
                },
                validate: function (value, params) {
                    return value.length > 128 || /^([a-zA-Z0-9._%+-]{1,64})@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value);
                }
            },
            mobile: {
                getMessage: function (fieldCode, params) {
                    return '手机格式不正确';
                },
                validate: function (value, params) {
                    return /^1[34578]\d{9}$/.test(value);
                }
            }
        };
    };

    function createDomain() {
        var randomdomain_with_domaincheck = createRandomString(6, 'lower case letters only');
        var charvalidation = /^[a-z]+$/;

        // check DomainName domain名は小文字のみ受け付ける。
        while (charvalidation.test(randomdomain_with_domaincheck) === false) {
            randomdomain_with_domaincheck = createRandomString(6, 'lower case letters only');
        }

        var devdomain = 'cndev' + randomdomain_with_domaincheck;
        return devdomain;
    }

    function createRandomString(num_of_chars, policy) {
        var randomstring = '';
        var allowedcharacters = '';

        switch (policy) {
            // 小文字のみの場合
            case 'lower case letters only':
                allowedcharacters = 'abcdefghijklmnopqrstuvwxyz';
                break;

            // 小文字+数字の場合
            case 'numbers and lower case letters':
                allowedcharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
                break;
        }

        for (var j = 0; j < num_of_chars; j++) {
            randomstring += allowedcharacters.charAt(Math.floor(Math.random() * allowedcharacters.length));
        }
        return randomstring;
    }

})(jQuery);